package com.cz.modules.cll.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cz.common.utils.HikvisionApiUtil;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.cll.dao.CllMonitoringPointDao;
import com.cz.modules.cll.entity.CllMonitoringPointEntity;
import com.cz.modules.cll.service.CllMonitoringPointService;


@Service("cllMonitoringPointService")
public class CllMonitoringPointServiceImpl extends ServiceImpl<CllMonitoringPointDao, CllMonitoringPointEntity> implements CllMonitoringPointService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CllMonitoringPointEntity> page = this.page(
                new Query<CllMonitoringPointEntity>().getPage(params),
                new QueryWrapper<CllMonitoringPointEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeAll() {
        this.baseMapper.removeAll();
    }

    @Override
    public void getPointList() throws ParseException {
        this.removeAll();
        Map maps=HikvisionApiUtil.monitoringPointDevice();
        if(maps!=null){
            Map map = (Map) JSON.parse(maps.get("data").toString());
            JSONArray datas = (JSONArray) JSON.parse(map.get("list").toString());
            List<CllMonitoringPointEntity> pointList=new ArrayList<>();
            for(int i = 0;i < datas.size();i++){
                JSONObject job = datas.getJSONObject(i);
                CllMonitoringPointEntity point=new CllMonitoringPointEntity();
                point.setIndexCode(job.get("indexCode").toString());
                point.setName(job.get("name").toString());
                point.setIp(job.get("ip").toString());
                point.setPort(job.get("port").toString());
                point.setResourceType(job.get("resourceType").toString());
                point.setRegionIndexCode(job.get("regionIndexCode").toString());
                point.setRegionIndexCode(job.get("regionIndexCode").toString());
                point.setUserName(job.get("userName").toString());
                point.setTreatyType(job.get("treatyType").toString());
                point.setNetZoneId(job.get("netZoneId").toString());
                point.setManufacturer(job.get("manufacturer").toString());
                point.setCapability(job.get("capability").toString());
                point.setCreateTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(job.get("createTime").toString()));
                point.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(job.get("updateTime").toString()));
                point.setSort(job.get("sort").toString());
                point.setDisOrder(job.get("disOrder").toString());
                point.setRegionName(job.get("regionName").toString());
                point.setRegionPath(job.get("regionPath").toString());
                pointList.add(point);
            }
            if(pointList.size()>0){
                this.saveBatch(pointList);
            }
        }
    }

}
